home *** CD-ROM | disk | FTP | other *** search
- Attribute VB_Name = "JDSAVER"
- '---------------------------------------------------------------------------
- 'This is VB4/32 sample code which produces a Windows95/NT4 screen saver. The
- 'program recognizes and properly deals with all of the command line parameters
- 'generated by the Display Options dialog, including running in the preview window
- 'and providing password dialogs. The actual screen saver is a simple colored
- 'disk paint and is provided for demonstrations purposes.
- '
- 'Two .OCX 32-bit custom controls are required but not included. They are
- 'COMCTL32.OCX, which is provided with VB4 Pro, and MSGHOO32.OCX, a message
- 'hooking control by Zane Thomas. Zane has allowed free distribution of the
- 'control, and it is widely available online, including Zane's web page at
- 'http://www.activexpert.com
- '
- '⌐1996 by Don Bradner and Jim Deutch. Freely distributable. Released in
- 'September, 1996. Don Bradner may be contacted at Compuserve 76130,1007 or
- 'dbirdman@arcatapet.com. Jim Deutch may be contacted at Compuserve 103134,3516.
- '
- 'No warranty is expressed or implied concerning usability or errors within this
- 'code. Bug reports should be made to Don Bradner at one of the addresses above.
- '
- 'If this program is updated, copies will normally be found in the VBPJFO and
- 'BASLANG forums on Compuserve, and via http://www.arcatapet.com/vb.html.
- '
- 'Compile this program to an .scr extension in
- 'your Windows\System directory. You will need to provide the .scr extension
- 'each time the program is compiled.
- '-----------------------------------------------------------------------------
- Option Explicit
-
- Type RECT 'Used by GetClientRect and GetWindowRect
- Left As Long
- Top As Long
- Right As Long
- Bottom As Long
- End Type
-
- '--------------------------------------------------------------------------
- 'API declarations
- '--------------------------------------------------------------------------
- Declare Function BitBlt& Lib "gdi32" (ByVal hDestDC&, ByVal x&, ByVal Y&, ByVal nWidth&, ByVal nHeight&, ByVal hSrcDC&, ByVal XSrc&, ByVal YSrc&, ByVal dwRop&)
- Declare Function CreateDC& Lib "gdi32" Alias "CreateDCA" (ByVal lpDriverName$, ByVal lpDeviceName$, ByVal lpOutput$, ByVal lpInitData&)
- Declare Function DeleteDC& Lib "gdi32" (ByVal hDC&)
- Declare Function FindWindow& Lib "user32" Alias "FindWindowA" (ByVal lpClassName$, ByVal lpWindowName$)
- Declare Function GetClientRect& Lib "user32" (ByVal hWnd&, lpRect As RECT)
- Declare Function GetWindowRect& Lib "user32" (ByVal hWnd&, lpRect As RECT)
- Declare Function GetWindowLong& Lib "user32" Alias "GetWindowLongA" (ByVal hWnd&, ByVal nIndex&)
- Declare Function IsWindow& Lib "user32" (ByVal hWnd&)
- Declare Function RegCloseKey& Lib "advapi32.dll" (ByVal HKey&)
- Declare Function RegOpenKeyExA& Lib "advapi32.dll" (ByVal HKey&, ByVal lpszSubKey$, dwOptions&, ByVal samDesired&, lpHKey&)
- Declare Function RegQueryValueExA& Lib "advapi32.dll" (ByVal HKey&, ByVal lpszValueName$, lpdwRes&, lpdwType&, ByVal lpDataBuff$, nSize&)
- Declare Function SendMessage& Lib "user32" Alias "SendMessageA" (ByVal hWnd&, ByVal wMsg&, ByVal wParam&, ByVal lParam As Any)
- Declare Function SetParent& Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long)
- Declare Function setWindowLong& Lib "user32" Alias "SetWindowLongA" (ByVal hWnd&, ByVal nIndex&, ByVal dwNewLong&)
- Declare Function SetWindowPos Lib "user32" (ByVal h&, ByVal hb&, ByVal x&, ByVal Y&, ByVal cx&, ByVal cy&, ByVal f&) As Integer
- Declare Function ShowCursor& Lib "user32" (ByVal bShow&)
- Declare Function StretchBlt& Lib "gdi32" (ByVal hDestDC&, ByVal x&, ByVal Y&, ByVal nWidth&, ByVal nHeight&, ByVal hSrcDC&, ByVal XSrc&, ByVal YSrc&, ByVal nSrcWidth&, ByVal nSrcHeight&, ByVal dwRop&)
-
- Public Const WM_CLOSE = &H10
- Private Const WM_USER = &H400
- Public Const EM_SETPASSWORDCHAR = WM_USER + 28
- Public Const ES_PASSWORD = &H20
- Public Const GWL_STYLE = -16
- Public Const SWP_NOMOVE = &H2
- Public Const SWP_NOSIZE = 1
- Public Const FLAGS = SWP_NOMOVE Or SWP_NOSIZE
- Public Const HWND_TOPMOST = -1
- Public Const SRCCOPY = &HCC0020
- Public Const SRCAND = &H8800C6
- Public Const SRCINVERT = &H660046
- Public Const HKEY_CURRENT_USER = &H80000001
-
- 'Registry Read permissions:
- Const KEY_QUERY_VALUE = &H1&
- Const KEY_ENUMERATE_SUB_KEYS = &H8&
- Const KEY_NOTIFY = &H10&
- Const READ_CONTROL = &H20000
- Const STANDARD_RIGHTS_READ = READ_CONTROL
- Const Key_Read = STANDARD_RIGHTS_READ Or KEY_QUERY_VALUE Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY
-
- Const REG_DWORD = 4& ' 32-bit number
-
-
- Public tempLong&
- Public tempString$
- Public tempInt%
- Public Password$
- Public PassChck%
- Public PWProtect%
- Public MouseMoves%
- Public PictureLoaded%
- Public CPWindow&
- Public CPRect As RECT
- Public xPixel%
- Public yPixel%
- Public Size%
- Public ScreenWidth%
- Public ScreenHeight%
- Function RegGetValue$(MainKey&, SubKey$, value$)
- ' MainKey must be one of the Publicly declared HKEY constants.
- Dim sKeyType& 'returns the key type. This function expects REG_SZ
- Dim ret& 'returned by registry functions, should be 0&
- Dim lpHKey& 'return handle to opened key
- Dim lpcbData& 'length of data in returned string
- Dim ReturnedString$ 'returned string value
- Dim fTempDbl!
- If MainKey >= &H80000000 And MainKey <= &H80000006 Then
- ' Open key
- ret = RegOpenKeyExA(MainKey, SubKey, 0&, Key_Read, lpHKey)
- If ret <> 0 Then
- RegGetValue = ""
- Exit Function 'No key open, so leave
- End If
-
- ' Set up buffer for data to be returned in.
- ' Adjust next value for larger buffers.
- lpcbData = 255
- ReturnedString = Space$(lpcbData)
-
- ' Read key
- ret& = RegQueryValueExA(lpHKey, value, ByVal 0&, sKeyType, ReturnedString, lpcbData)
- If ret <> 0 Then
- RegGetValue = "" 'Key still open, so finish up
- Else
- If sKeyType = REG_DWORD Then
- fTempDbl = Asc(Mid$(ReturnedString, 1, 1)) + &H100& * Asc(Mid$(ReturnedString, 2, 1)) + &H10000 * Asc(Mid$(ReturnedString, 3, 1)) + &H1000000 * CDbl(Asc(Mid$(ReturnedString, 4, 1)))
- ReturnedString = Format$(fTempDbl, "000")
- End If
- RegGetValue = Left$(ReturnedString, lpcbData - 1)
- End If
- ' Always close opened keys!
- ret = RegCloseKey(lpHKey)
- End If
- End Function
-
- Sub Centerform(FrmName As Form)
- FrmName.Top = Screen.Height / 2 - FrmName.Height / 2
- FrmName.Left = Screen.Width / 2 - FrmName.Width / 2
- End Sub
-
- Sub CopyScreen(canvas As Object)
- Dim screendc&
- canvas.AutoRedraw = True
- screendc = CreateDC("DISPLAY", "", "", 0&)
- tempLong = StretchBlt(canvas.hDC, 0, 0, canvas.Width, canvas.Height, screendc, 0, 0, Screen.Width, Screen.Height, SRCCOPY)
- tempLong = DeleteDC(screendc)
- canvas.AutoRedraw = False
-
- End Sub
-
- Public Function encrypt$(passString$)
- 'This is a simple encryption method for passwords so that they
- 'do not appear in the registry in plain text. Changing the values
- '14 and 4 below would result in a different encryption.
- Dim x%
- If passString = "" Then
- encrypt = ""
- Exit Function
- End If
- passString = UCase(passString)
- If Len(passString) > 20 Then passString = Left$(passString, 20)
- tempString = Space$(Len(passString))
- For x = 1 To Len(passString)
- tempInt = Asc(Mid$(passString, x, 1))
- tempInt = tempInt + 14 + (4 * x)
- 'Shift all values to occur in the printable lower 128 ascii
- 'characters.
- Do While tempInt > 126
- tempInt = tempInt - 126
- Loop
- Do While tempInt < 33
- tempInt = tempInt + 33
- Loop
- Mid$(tempString, x, 1) = Chr$(tempInt)
- Next x
- encrypt = tempString
-
- End Function
-
-
- Public Sub Draw(canvas As Object)
- 'This small sub is the actual screen saver. This sample
- 'just draws colored circles on the screen.
- Dim x As Integer
- Dim Y As Integer
- Dim radius As Integer
- Dim Colr As Long
- Dim i As Integer
- ScreenWidth = canvas.Width
- ScreenHeight = canvas.Height
- 'Draw circles
- For i = 1 To 200 / Size / Size 'Many small or fewer large circles
- x = Rnd * ScreenWidth
- Y = Rnd * ScreenHeight
- Colr = Rnd * &HFFFFFF
- radius = Rnd * ScreenWidth / 400 * Size * Size
- canvas.FillColor = Colr
- canvas.FillStyle = vbFSSolid
- canvas.Circle (x, Y), radius, Colr
- Next i
- End Sub
-
- Sub main()
- 'We start the screen saver from a sub main which arbitrates
- 'the command line parameter and loads an appropriate form.
- Dim StartType$
- xPixel = Screen.TwipsPerPixelX
- yPixel = Screen.TwipsPerPixelY
-
- 'Get the user's previous preference for Circle size, with a
- 'default of half-size.
- Size = Val(GetSetting("Samples", "JD Screen Saver", "Size", "5"))
- 'Make sure we are within allowable range.
- If Size < 1 Then Size = 1
- If Size > 9 Then Size = 9
-
- StartType = UCase(Left$(Command, 2))
- Select Case StartType
- Case "/C"
- Configuration.Show
- Case "/S"
- '----------------------------------------------
- 'The system may start more than one screensaver
- 'session, so we need to check for a previous
- 'instance. The problem is that if the previous
- 'instance is a "/P" instance, the control panel
- 'will not close that instance before this one
- 'starts. Therefore we can't use App.Previnstance.
- 'This routine looks for the Main form and exits if
- 'it is present.
- '----------------------------------------------
- If CheckUnique("Screen Saver Main Form") = False Then
- Exit Sub
- End If
- MainForm.Show
- Case "/P"
- 'A handle to the Preview window is passed following the
- '/p. We will use this handle to place our output.
- CPWindow = Val(Right$(Command, Len(Command) - 2))
- Load ControlForm
- Case "/A"
- 'A handle to the Display Properties main window is passed
- 'following the /a. We will use this handle to place the
- 'password configuration over the window.
- CPWindow = Val(Right$(Command, Len(Command) - 2))
- PassChange.Show
-
- End Select
- End Sub
- Function CheckUnique%(FormCaption$)
- 'looks for a window with the same caption
- Dim HandleWin&
- HandleWin = FindWindow(vbNullString, FormCaption)
- If HandleWin = 0 Then
- CheckUnique = True
- Else
- CheckUnique = False
- End If
- End Function
-
-